/**
 * 
 */
package com.wubo.sec.model;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.util.DateHelper;

/**
 * 已经删除的用户
 * @author WuBo
 * @CreateDate 2012-4-20
 */
@Entity
@Table(name="SEC_USER_RECYCLE")
public class DeletedUser {
private static final long serialVersionUID = 8105509037133087632L;
	@Id
	@Column(unique=true, nullable=true, length=32)
	private String username; //登录名
	
	@Column(nullable=true, length=32)
	private String password; //密码
	
	@Column(length=50)
	private String truename; //显示名
	
	@Column(length=50)
	private String nickname; //昵称
	
	@Column(length=100)
	private String email; //Email

	private Date createTime; //创建时间
	private String createUser; //创建用户
	private Date updateTime; //修改时间
	private String updateUser; //修改用户
	
	private int loginTimes; //登录次数
	
	private Date lastLoginTime; //上次登录时间
	
	@Column(length=20)
	private String lastLoginIP; //上次登录IP
	
	@Column(length=255)
	private String headAddr; //用户头像地址
	
	@Column(nullable=false)
	private boolean active; //是否可用
	
	private boolean locked; //是否已锁定
	
	private Date expiredDate; //失效日期
	
	@Column(name="OWNER_REL")
	private String owner; //所有者
	
	@ManyToOne(cascade={CascadeType.REFRESH,CascadeType.MERGE},fetch=FetchType.EAGER)
	@JoinColumn(name="GROUP_REL")
	private Group group; //用户组
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public byte[] getSalt() {
		return username.getBytes();
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getExpiredDate() {
		return expiredDate;
	}
	public void setExpiredDate(Date expiredDate) {
		this.expiredDate = expiredDate;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getCreateUser() {
		return createUser;
	}
	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	public String getUpdateUser() {
		return updateUser;
	}
	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}
	public int getLoginTimes() {
		return loginTimes;
	}
	public void setLoginTimes(int loginTimes) {
		this.loginTimes = loginTimes;
	}
	public Date getLastLoginTime() {
		return lastLoginTime;
	}
	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}
	public String getLastLoginIP() {
		return lastLoginIP;
	}
	public void setLastLoginIP(String lastLoginIP) {
		this.lastLoginIP = lastLoginIP;
	}
	public String getHeadAddr() {
		return headAddr;
	}
	public void setHeadAddr(String headAddr) {
		this.headAddr = headAddr;
	}
	public boolean isActive() {
		return active;
	}
	public void setActive(boolean active) {
		this.active = active;
	}
	public boolean isLocked() {
		return locked;
	}
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	
	public String getLastLoginTimeText(){
		return DateHelper.getDayToString(lastLoginTime);
	}
	
	public String getTruename() {
		return truename;
	}
	public void setTruename(String truename) {
		this.truename = truename;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUsername() {
		return username;
	}

	public boolean isAccountNonExpired() {
		if(expiredDate == null){
			return true;
		}
		return expiredDate.after( new java.util.Date() );
	}

	public boolean isAccountNonLocked() {
		return !locked;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return active;
	}
	
	public String getUserText(){
		String name = StringUtils.isEmpty(nickname) ? truename : nickname;
		return StringUtils.isEmpty(name) ? username : name;
	}
	
	public String toString(){
		return getUserText();
	}
	public String getOrgCode() {
		return null;
	}
	public String getOrgName() {
		return null;
	}
	
	public User clone() {
		try{
			return (User) super.clone();
		}catch(CloneNotSupportedException e){
			return null;
		}
	}
	
	@Override
	public int hashCode(){
		return getUsername().hashCode();
	}
	
	@Override
	public boolean equals(Object obj){
		if(obj != null && obj instanceof User){
			User user = (User) obj;
			return this.getUsername().equals(user.getUsername());
		}
		return false;
	}
}
